<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var isAdditiveNumber = function (num) {
      function match(l, m, r) {
        // 处理边界情况、特殊情况（0开头的数）
        if (r > num.length || m > num.length - 1 || (num[l] === '0' && m - l > 1)) return false;
        if (m >= r) return match(l, m, r + 1);
        if (num[m] === '0' && r - m > 1) return match(l, m + 1, r);

        const n3 = `${+num.slice(l, m) + +num.slice(m, r)}`;
        // 剪枝：如果剩余字符比要查找的结果更短，直接返回false
        if (n3.length > num.length - r) return false;
        // 匹配成功 🚀🚀🚀
        if (num.slice(r, r + n3.length) === n3 && (r + n3.length === num.length || match(m, r, r + n3.length))) return true;

        // 匹配失败：尝试查找其他可能性
        return match(l, m, r + 1) || match(l, m + 1, r);
      }
      return match(0, 1, 2);
    };
  </script>
</body>

</html>